<template>
  <li>
    <label>
      <input type="checkbox" v-model="todo.isDone" />
      <span>{{ todo.name }}</span>
    </label>
    <button class="btn btn-danger" @click="delTodo">删除</button>
  </li>
</template>

<script lang="ts">
export default {
  name: "Item",
};
</script>

<script lang="ts" setup>
import type { TodoItem } from "../../../pinia/modules/models/todoModel";
import { useTodoStore } from "../../../pinia/modules/todo";

// 声明接受props数据
const props = defineProps<{
  todo: TodoItem;
}>();

const todoStore = useTodoStore();

// 删除todo
const delTodo = () => {
  // filter方法中函数返回true保留，返回false是过滤
  // filter方法不会修改原数组（原数据前后值不变）
  todoStore.todoList = todoStore.todoList.filter((todo) => todo.id !== props.todo.id);
};
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:hover button {
  display: block;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
</style>
